// pages/publish/carPublish.js

var Api = require('../../utils/api.js');
var wxRequest = require('../../utils/wxRequest.js');
var util = require('../../utils/util.js');

var today = util.formatTime(new Date((new Date()).getTime() + (1000 * 60 * 60 * 24 * 1))).split(' ')[0];
var minday = util.formatTime(new Date()).split(' ')[0];
var maxday = util.formatTime(new Date((new Date()).getTime() + (1000 * 60 * 60 * 24 * 62))).split(' ')[0];

Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: 0,
    departure: '', //出发地
    destination: '', //目的地
    date: today,     //今天
    start: minday,  
    end: maxday,
    time: '00:00',
    carpool_desc: '',
    Vacancys: [0, 1, 2, 3, 4, 5, 6, 7, 8], 
    vacancy: 0,
    btn_disabled: false,
    carPoolTypeArr: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var self = this;
    self.getCategries();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },

  //获取拼车分类
  getCategries: function () {
    var self = this;
    var getCarCategoriesRequest = wxRequest.getRequest(Api.getCarCategories());
    getCarCategoriesRequest.then(response => {
      if (response.statusCode == 200 && response.data.code == 200) {
        if (response.data.data && response.data.data.length > 0) {
          var res = response.data.data;
          self.setData({
            carPoolTypeArr: res,
            carpool_desc: res[0].description
          })
        }
      }
    })
  },

  //分类选择回调
  bindTypeChange : function (e) {
    var that = this;
    that.setData({ 
      index: e.detail.value,
      carpool_desc: that.data.carPoolTypeArr[e.detail.value].description
    })
  },

  //选择出发地
  chooseDep: function (e) {
    var that = this;
    wx.chooseLocation({
      success: function (res) {
        that.setData({
          departure: res.address
        })
      }
    })
  },

  //选择目的地
  chooseDes: function() {
    var that = this;
    wx.chooseLocation({
      success: function (res) {
        that.setData({
          destination: res.address
        })
      }
    })    
  },

  //时间选择
  bindTimeChange: function (e) {
    this.setData({
      time: e.detail.value
    })
  },

  //日期选择
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },

  //选择人数
  bindVacancyChange: function (e) {
    this.setData({ 
      vacancy: e.detail.value 
    })
  },

  //免责声明checkbox选择
  checkChange: function (e) {
    this.setData({
      btn_disabled: !this.data.btn_disabled
    })
  },

  //发布
  carFormSubmit: function (e) {
    var that = this;
    var formobj = e.detail.value;
    var submit_carpool_type = formobj.carpool_type; //发布类型
    var submit_cfd = formobj.cfd; //出发地
    var submit_mdd = formobj.mdd; //目的地
    var submit_date = formobj.date; //出发日期
    var submit_time = formobj.time; //出发时间
    var submit_vacancy = formobj.vacancy; //人数
    var submit_lxr = formobj.lxr; //联系人
    var submit_sjh = formobj.sjh; //手机号
    var submit_remark = formobj.remark; //备注
    if (submit_cfd == '' && !submit_cfd) {
      wx.showModal({
        title: '提示',
        content: '出发地不能为空!',
        showCancel: false
      })
      return;
    }
    if (submit_mdd == '' && !submit_mdd) {
      wx.showModal({
        title: '提示',
        content: '目的地不能为空!',
        showCancel: false
      })
      return;
    }
    if (submit_lxr == '' && !submit_lxr) {
      wx.showModal({
        title: '提示',
        content: '联系人不能为空!',
        showCancel: false
      })
      return;
    }
    if (submit_sjh == '' && !submit_sjh) {
      wx.showModal({
        title: '提示',
        content: '手机号不能为空!',
        showCancel: false
      })
      return;
    }
    if (!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(submit_sjh))) {
      wx.showModal({
        title: '提示',
        content: '手机号码格式不正确!',
        showCancel: false
      })
      return;
    }
    var data = {
      category_id: submit_carpool_type,
      departure: submit_cfd,
      destination: submit_mdd,
      departure_date: submit_date,
      departure_time: submit_time,
      contacts_name:submit_lxr,
      contacts_cell:submit_sjh,
      vacancy: submit_vacancy,
      remark: submit_remark
    };
    wx.showLoading({
      title: '信息发布中...',
      mask: true
    });      
    var createCarpoolUrl = Api.createCarPool();   //接口url
    var postCreateCarpoolRequest = wxRequest.postRequest(createCarpoolUrl, data);
    postCreateCarpoolRequest.then(res => {
      if (res.statusCode == 200 && res.data.code == 200 ) {
        console.log('创建成功');
        wx.hideLoading();
      }else{
        console.log(res.data.msg);
      }
    })
  }
})